package com.ctshk.sapp.goods.controller;


import cn.hutool.json.JSONObject;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.goods.dto.CommodityClassificationAllLevelListDTO;
import com.ctshk.rpc.goods.dto.CommodityClassificationDTO;
import com.ctshk.rpc.goods.req.CommodityClassificationAddReq;
import com.ctshk.rpc.goods.req.CommodityClassificationPageReq;
import com.ctshk.rpc.goods.service.ICommodityClassificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-04-08
 */
@Api(value="商品管理-商品分类", tags="商品管理-商品分类")
@RestController
@RequestMapping("/commodity/classification")
public class CommodityClassificationController extends BaseController {
	
	@DubboReference
    private ICommodityClassificationService classificationService;

    @ApiOperation(value = "新增商品分类", notes = "新增商品分类")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增商品分类", operDesc = "新增商品分类")
    @PostMapping("/addCommodityClassification")
    public Result addCommodityClassification(@RequestBody @Valid CommodityClassificationAddReq obj) throws Exception {
    	classificationService.addCommodityClassification(obj, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "编辑商品分类", notes = "编辑商品分类")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "编辑商品分类", operDesc = "编辑商品分类")
    @PostMapping("/updateCommodityClassification")
    public Result updateCommodityClassification(@RequestBody @Valid CommodityClassificationAddReq obj) throws Exception {
    	classificationService.updateCommodityClassification(obj,getCurrentUser(getRequest()).getId());
        return Result.success();
    }
    
    @ApiOperation(value = "分页查询商品分类管理", notes = "分页查询商品分类管理")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "分页查询商品分类管理", operDesc = "分页查询商品分类管理")
    @PostMapping("/findPageList")
    public Result<PageResponse<CommodityClassificationDTO>> findPageList(@RequestBody CommodityClassificationPageReq pageReq) {
        PageResponse<CommodityClassificationDTO> response = classificationService.findCommodityClassification(pageReq,getCurrentUser(getRequest()));
        return Result.success(response);
    }
    
    @ApiOperation(value = "查询一级分类名称(上级)下拉列表", notes = "查询一级分类名称(上级)下拉列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询一级分类名称(上级)下拉列表", operDesc = "查询一级分类名称(上级)下拉列表")
    @GetMapping("/findClassificationNameList/{menuId}")
    public Result<List<CommodityClassificationDTO>> findClassificationNameList(@PathVariable("menuId") Long menuId) {
    	List<CommodityClassificationDTO> response = classificationService.findClassificationNameList(menuId, getCurrentUser(getRequest()));
        return Result.success(response);
    }

    @ApiOperation(value = "查询一二级下拉列表", notes = "查询一二级下拉列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询一二级下拉列表", operDesc = "查询一二级下拉列表")
    @GetMapping("/findClassificationByAllLevel/{menuId}")
    public Result<List<CommodityClassificationAllLevelListDTO>> findClassificationByAllLevel(@PathVariable("menuId") Long menuId) {
        List<CommodityClassificationAllLevelListDTO> response = classificationService.findClassificationByAllLevel(null, menuId, getCurrentUser(getRequest()));
        return Result.success(response);
    }

    @ApiOperation(value = "更新分类状态", notes = "更新分类状态")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "更新分类状态", operDesc = "更新分类状态")
    @PostMapping("/updateClassficationStatus")
    public Result<Boolean> updateClassficationStatus(@RequestBody CommodityClassificationAddReq req) {
        Boolean flag = classificationService.updateClassfincationStatus(req);
        return Result.success(flag);
    }

    @ApiOperation(value = "查询分类列表", notes = "查询分类列表")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "查询分类列表", operDesc = "查询分类列表")
    @PostMapping("/findCommodityClassficationListAll/{menuId}")
    public Result<List<JSONObject>> findCommodityClassficationListAll(@PathVariable("menuId") Long menuId) {
        List<JSONObject> jsonObjectList = classificationService.findCommodityClassficationListAll(menuId, getCurrentUser(getRequest()));
        return Result.success(jsonObjectList);
    }
}
